import { Random } from "mockjs";
import { defineStore } from "pinia";
import { computed, reactive, ref } from "vue";



const useIdeasData = defineStore('ideas', () => {
    const ideas = ref([
        {
            id: 1,
            title: '管理者必读',
            channel: '团队建设',
            content: '简介',
            checked: '是',
            imageUrl: '',
            sort: '5',
            detail: '观点详情',
            time: '2022.8.21',
        },
        {
            id: 2,
            title: '管理者必读',
            channel: '团队建设',
            content: '简介',
            checked: '是',
            imageUrl: '',
            sort: '5',
            detail: '观点详情',
            time: '2022.8.21',
        },
        {
            id: 3,
            title: '管理者必读',
            channel: '团队建设',
            content: '简介',
            checked: '是',
            imageUrl: '',
            sort: '5',
            detail: '观点详情',
            time: '2022.8.21',
        },
        {
            id: 4,
            title: '管理者必读',
            channel: '团队建设',
            content: '简介',
            checked: '是',
            imageUrl: '',
            sort: '5',
            detail: '观点详情',
            time: '2022.8.21',
        },
        {
            id: 5,
            title: '管理者必读',
            channel: '团队建设',
            content: '简介',
            checked: '是',
            imageUrl: '',
            sort: '5',
            detail: '观点详情',
            time: '2022.8.21',
        },
        {
            id: 6,
            title: '管理者必读',
            channel: '团队建设',
            content: '简介',
            checked: '是',
            imageUrl: '',
            sort: '5',
            detail: '观点详情',
            time: '2022.8.21',
        },

    ],
    )

    const total = ideas.value.length




    const addIdeaData = (obj) => {
        const res = ideas.value.find(item => item.id == obj.id)
        console.log(res);
        if (res) {
            res.id = obj.id
            res.title = obj.title
            res.content = obj.content
            res.checked = obj.checked
            res.sort = obj.sort
            res.time = obj.time
            res.detail = obj.detail
        } else {
            ideas.value.push(obj)
        }

    }




    const findIdea = (id) => {
        const res = ideas.value.find(item => item.id == id)
        console.log(res)
        return res
    }
    const findIdeaData = (title) => {
        const res = ideas.value.filter(item => item.title === title)
        return res
    }

    const findIdeaChannel = (channel) => {
        console.log(channel)
        const res = ideas.value.filter(item => item.channel == channel)
        console.log(res)
        return res
    }

    return {
        ideas,
        total,
        addIdeaData,
        findIdea,
        findIdeaData,
        findIdeaChannel,
    }
}, {
    persist: true
})


export default useIdeasData